fix: use a safer gsub_file and update/remove file gsubs that were no longer doing anything#533
Conversation
G-Rath
commented
Feb 19, 2024
Comment on lines
-18
to
-20
| gsub_file "config/environments/development.rb", | ||
| "join('tmp', 'caching-dev.txt')", | ||
| 'join("tmp/caching-dev.txt")' |
Contributor
Author
There was a problem hiding this comment.
note: this is no longer matching because of the quotes, but Rubocop can automatically fix this anyway now
G-Rath
commented
Feb 19, 2024
| config.force_ssl = ENV.fetch("RAILS_FORCE_SSL", "true").downcase != "false" | ||
| RUBY | ||
| gsub_file! "config/environments/production.rb", | ||
| "config.force_ssl = true", |
Contributor
Author
There was a problem hiding this comment.
note: Rails 7.1 now ships with this just enabled, but I think it's worth us keeping an env variable for toggling it just in case
G-Rath
commented
Feb 19, 2024
| gsub_file "config/initializers/filter_parameter_logging.rb", /\[:password\]/ do | ||
| "%w[password secret session cookie csrf]" | ||
| end | ||
| gsub_file! "config/initializers/filter_parameter_logging.rb", |
Contributor
Author
There was a problem hiding this comment.
note: this relates to #529 - technically I've fixed this gsub so we're now adding to the existing values, but we still want to re-review our options
G-Rath
commented
Feb 19, 2024
Comment on lines
-1
to
-3
| gsub_file "config/environments/test.rb", | ||
| "config.eager_load = false", | ||
| "config.eager_load = defined?(SimpleCov).present?" |
Contributor
Author
There was a problem hiding this comment.
note: Rails 7.1 has changed this to ENV["CI"].present? rather than true
I don't think it's better to check if SimpleCov is defined but maybe I'm wrong?
b50397d to
790a0f7
Compare
eoinkelly
approved these changes
Feb 22, 2024
lukeify
approved these changes
Mar 7, 2024
G-Rath
added a commit
that referenced
this pull request
Mar 27, 2025
The file manipulation methods provided by Thor don't stop execution when they fail to do the defined manipulation, instead in our case they print an error but then proceed; we actually want to error in this situation since the whole point of our template is to be applying our preferred practices meaning its moot if we don't... This really came into light with the Rails 8 upgrade which had a number of changes to the configuration files in particular that caused a number of our customizations to be skipped. By introducing bang versions of the methods that error if the file contents does not change, we can ensure going forward all manipulations actually work; this uses the same logic I introduced in #533 for `gsub_file`, and caught a bug that I fixed in #596
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
So it turns out that
gsub_filedoes not actually check if it matched anything and so we have a few misc. changes silently not being applied due to changes in Rails 7.1.This addresses that by switching us to use
gsub_file!which reads the file into memory before it's gsub'd and then compares the results to make sure it actually changed.I've opened rails/thor#874 to add this to Thor itself